package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.PigDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;
import cn.edu.scau.cmi.hombio.dao.TaskPigDAO;

import cn.edu.scau.cmi.hombio.domain.Pig;
import cn.edu.scau.cmi.hombio.domain.Task;
import cn.edu.scau.cmi.hombio.domain.TaskPig;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for TaskPig entities
 * 
 */

@Service("TaskPigService")

@Transactional
public class TaskPigServiceImpl implements TaskPigService {

	/**
	 * DAO injected by Spring that manages Pig entities
	 * 
	 */
	@Autowired
	private PigDAO pigDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * DAO injected by Spring that manages TaskPig entities
	 * 
	 */
	@Autowired
	private TaskPigDAO taskPigDAO;

	/**
	 * Instantiates a new TaskPigServiceImpl.
	 *
	 */
	public TaskPigServiceImpl() {
	}

	/**
	 * Save an existing Pig entity
	 * 
	 */
	@Transactional
	public TaskPig saveTaskPigPig(Integer id, Pig related_pig) {
		TaskPig taskpig = taskPigDAO.findTaskPigByPrimaryKey(id, -1, -1);
		Pig existingpig = pigDAO.findPigByPrimaryKey(related_pig.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpig != null) {
			existingpig.setId(related_pig.getId());
			existingpig.setNumber(related_pig.getNumber());
			existingpig.setEarNumber(related_pig.getEarNumber());
			existingpig.setRfid(related_pig.getRfid());
			existingpig.setBirthday(related_pig.getBirthday());
			existingpig.setState(related_pig.getState());
			existingpig.setMemo(related_pig.getMemo());
			related_pig = existingpig;
		}

		taskpig.setPig(related_pig);
		related_pig.getTaskPigs().add(taskpig);
		taskpig = taskPigDAO.store(taskpig);
		taskPigDAO.flush();

		related_pig = pigDAO.store(related_pig);
		pigDAO.flush();

		return taskpig;
	}

	/**
	 * Return all TaskPig entity
	 * 
	 */
	@Transactional
	public List<TaskPig> findAllTaskPigs(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<TaskPig>(taskPigDAO.findAllTaskPigs(startResult, maxRows));
	}

	/**
	 * Delete an existing Pig entity
	 * 
	 */
	@Transactional
	public TaskPig deleteTaskPigPig(Integer taskpig_id, Integer related_pig_id) {
		TaskPig taskpig = taskPigDAO.findTaskPigByPrimaryKey(taskpig_id, -1, -1);
		Pig related_pig = pigDAO.findPigByPrimaryKey(related_pig_id, -1, -1);

		taskpig.setPig(null);
		related_pig.getTaskPigs().remove(taskpig);
		taskpig = taskPigDAO.store(taskpig);
		taskPigDAO.flush();

		related_pig = pigDAO.store(related_pig);
		pigDAO.flush();

		pigDAO.remove(related_pig);
		pigDAO.flush();

		return taskpig;
	}

	/**
	 * Delete an existing Task entity
	 * 
	 */
	@Transactional
	public TaskPig deleteTaskPigTask(Integer taskpig_id, Integer related_task_id) {
		TaskPig taskpig = taskPigDAO.findTaskPigByPrimaryKey(taskpig_id, -1, -1);
		Task related_task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		taskpig.setTask(null);
		related_task.getTaskPigs().remove(taskpig);
		taskpig = taskPigDAO.store(taskpig);
		taskPigDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		taskDAO.remove(related_task);
		taskDAO.flush();

		return taskpig;
	}

	/**
	 * Save an existing TaskPig entity
	 * 
	 */
	@Transactional
	public void saveTaskPig(TaskPig taskpig) {
		TaskPig existingTaskPig = taskPigDAO.findTaskPigByPrimaryKey(taskpig.getId());

		if (existingTaskPig != null) {
			if (existingTaskPig != taskpig) {
				existingTaskPig.setId(taskpig.getId());
				existingTaskPig.setMemo(taskpig.getMemo());
			}
			taskpig = taskPigDAO.store(existingTaskPig);
		} else {
			taskpig = taskPigDAO.store(taskpig);
		}
		taskPigDAO.flush();
	}

	/**
	 * Load an existing TaskPig entity
	 * 
	 */
	@Transactional
	public Set<TaskPig> loadTaskPigs() {
		return taskPigDAO.findAllTaskPigs();
	}

	/**
	 * Return a count of all TaskPig entity
	 * 
	 */
	@Transactional
	public Integer countTaskPigs() {
		return ((Long) taskPigDAO.createQuerySingleResult("select count(o) from TaskPig o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing Task entity
	 * 
	 */
	@Transactional
	public TaskPig saveTaskPigTask(Integer id, Task related_task) {
		TaskPig taskpig = taskPigDAO.findTaskPigByPrimaryKey(id, -1, -1);
		Task existingtask = taskDAO.findTaskByPrimaryKey(related_task.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtask != null) {
			existingtask.setId(related_task.getId());
			existingtask.setTaskNumber(related_task.getTaskNumber());
			existingtask.setCreateTime(related_task.getCreateTime());
			existingtask.setStartTime(related_task.getStartTime());
			existingtask.setEndTime(related_task.getEndTime());
			existingtask.setMemo(related_task.getMemo());
			related_task = existingtask;
		}

		taskpig.setTask(related_task);
		related_task.getTaskPigs().add(taskpig);
		taskpig = taskPigDAO.store(taskpig);
		taskPigDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		return taskpig;
	}

	/**
	 * Delete an existing TaskPig entity
	 * 
	 */
	@Transactional
	public void deleteTaskPig(TaskPig taskpig) {
		taskPigDAO.remove(taskpig);
		taskPigDAO.flush();
	}

	/**
	 */
	@Transactional
	public TaskPig findTaskPigByPrimaryKey(Integer id) {
		return taskPigDAO.findTaskPigByPrimaryKey(id);
	}
}
